package net.usys.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.usys.entity.Admin;
import net.usys.service.AdminService;
import net.usys.service.impl.AdminServiceImpl;

/**
 * 非正常登录用户的请求后台过滤
 * @author acer
 *
 */

public class AdminLoginFilter implements Filter {

	public static final String ADMIN_CAPTCHA_LOGIN_URL = "/admin/admin!login.action";// 非法用户请求后台跳转URL
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
	throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		boolean isCaptcha = false;
		Object obj = request.getSession().getAttribute(("SPRING_SECURITY_LAST_USERNAME"));
		if (null!=obj) {
			isCaptcha = true;
		}
		if (isCaptcha) {
			chain.doFilter(request, response);
		} else {
			response.sendRedirect(request.getContextPath() + ADMIN_CAPTCHA_LOGIN_URL);
		}

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}
}
